require "vector"
require "std"
require "d12"

function d10()
  local object={}
  -- every object needs a mass, angular mass, position, angular velocity, etc, etc.
  object.position=Vector{0,0,0}
  object.mass=1
  object.velocity=Vector{0,0,0}
  object.angularMass=4
  object.angularVelocity=Vector{0,0,0}
  object.color=0xFFFF00
  object.radius=0.9
  
  -- we take a d12 meld two opposite faces together
  local d12=d12()
  object.vertices=d12.vertices

  --first surface is random
  for i, vertex in ipairs(d12.surfaces[1].polygon) do table.remove(object.vertices,table.index(object.vertices,vertex)) end
  local top=d12.surfaces[1].center*2.23605
  table.insert(object.vertices,top)
  
  --the opposite surface
  local opposite
  for i, surface in ipairs(d12.surfaces) do if vector.dot(d12.surfaces[1].normal,surface.normal)<-0.999 then opposite=surface end end
  for i, vertex in ipairs(opposite.polygon) do table.remove(object.vertices,table.index(object.vertices,vertex)) end
  local bottom=opposite.center*2.23605
  table.insert(object.vertices,bottom)
  
  --we also shrink the entire system in the top-bottom direction
  local base=vector.unit(top)
  local ratio=0.6
  for i, vertex in ipairs(object.vertices) do
    local size=vector.dot(vertex,base)
    vertex[1],vertex[2],vertex[3]=unpack(vertex+(ratio-1)*base*size)
  end
  
  
  -- every object has a set of surfaces that hold the polygons and the textures
  local polygons=verticesToPolygons(object.vertices)
  object.surfaces=table.transform(polygons,polygonToSurface)
  
  --every surface also has a texture that contains the number image.
  for i,surface in ipairs(object.surfaces) do
    local top=table.index(surface.polygon,top) or table.index(surface.polygon,bottom)
    surface.texture={
      image=(i%10)..".png",
      o=math.avg(surface.center),--,surface.polygon[math.mod1(top+2,4)]),
      --y=math.avg(surface.polygon[top], surface.center)
      y=math.avg(surface.center,surface.polygon[math.mod1(top+2,4)])
    }
    surface.texture.x=vector.cross(surface.normal, (surface.texture.y-surface.texture.o))+surface.texture.o
  end
  
  -- every object also has a set of vectors that need to be updated with rotation.
  object.allVertices=extractAllVertices(object)

  -- also surfaces refer back to their parent object
  for i,surface in ipairs(object.surfaces) do 
    surface.object=object
  end
  
return object end

function d100() --this is just a retextured d10
  local object=d10()
  for i,surface in ipairs(object.surfaces) do
    surface.texture.image="1"..(i%10).."0.png"
  end
return object end
